package com.naiterui.ehp.bs.esign.api.bjca.service.impl;

import com.naiterui.ehp.bp.constants.EsignServiceProviderEnum;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bp.utils.beans.BeanUtil;
import com.naiterui.ehp.bs.esign.api.ISignService;
import com.naiterui.ehp.bs.esign.api.bjca.util.SignYwxUtil;
import com.naiterui.ehp.bs.esign.api.bjca.vo.YwxSignParamVO;
import com.naiterui.ehp.bs.esign.api.bjca.vo.YwxUserVO;
import com.naiterui.ehp.bs.esign.api.vo.SignAccountVO;
import com.naiterui.ehp.bs.esign.api.vo.SignFileParamVO;
import com.naiterui.ehp.bs.esign.api.vo.SignUserInfoVO;
import com.naiterui.ehp.bs.esign.api.vo.UserInfoParamVO;
import com.naiterui.ehp.bs.esign.service.IEsignService;
import com.naiterui.ehp.bs.esign.vo.esign.PreSignResultVO;
import lombok.extern.slf4j.Slf4j;

/**
 * @Description 医网签签名实现
 * @Author gongbaoqiang
 * @Date 2019/12/23 7:38 PM
 */
@Slf4j
public class SignServiceImpl implements ISignService {

    @Override
    public EsignServiceProviderEnum getCurrentServiceProvider() {
        return EsignServiceProviderEnum.BJCA;
    }

    @Override
    public SignUserInfoVO registAccount(UserInfoParamVO userInfoParamVO) throws BusinessException {
        log.info("医网签同步用户信息：userinfoParamVO={}", userInfoParamVO.toString());
        YwxUserVO resultVO = SignYwxUtil
            .synUser(userInfoParamVO.getName(), userInfoParamVO.getIdNo(), userInfoParamVO.getPhone(), userInfoParamVO.getOrgan(),
                userInfoParamVO.getTitle());
        log.info("医网签审核结果通过回调接口方式实现");
        return SignUserInfoVO.builder().openId(resultVO.getOpenId()).build();
    }

    @Override
    public SignUserInfoVO updateAccount(UserInfoParamVO userInfoParamVO) throws BusinessException {
        log.info("医网签更新和新增用户使用同一个接口");
        return this.registAccount(userInfoParamVO);
    }

    @Override
    public String getRealnameCode(String name, String phone, String idCard) throws BusinessException {
        log.info("医网签用户无单独实名认证接口");
        return "";
    }

    @Override
    public void deleteAccount(String accountId) throws BusinessException {
        log.info("删除签名账号：accountId={}", accountId);
    }

    @Override
    public boolean checkRealnameCode(String phone, String code) throws BusinessException {
        log.info("医网签用户注册和实名认证同时完成");
        return true;
    }

    @Override
    public String createSeal(String accountId, String name, String typeface) throws BusinessException {
        log.info("医网签目前不支持主动生成签名");
        return "";
    }

    @Override
    public PreSignResultVO preSign(SignFileParamVO signParamVO, SignAccountVO signAccountVO) throws BusinessException {
        YwxSignParamVO ywxSignParamVO = new YwxSignParamVO();
        BeanUtil.copyProperties(signParamVO, ywxSignParamVO);
        ywxSignParamVO.setOpenId(signAccountVO.getOpenId());
        //去掉参数中的关键字，统一按坐标签名
        ywxSignParamVO.setKeyword("");
        String uniqueId = SignYwxUtil.synPdf(ywxSignParamVO);
        return PreSignResultVO.builder().uniqueId(uniqueId).build();
    }

    @Override
    public void sign(SignFileParamVO signParamVO, SignAccountVO signAccountVO, IEsignService callback) throws BusinessException {
        log.info("医网信通过回调方式生成签名结果，此处不进行处理");
    }

    @Override
    public void resetSignPwd(String openId, String newPassword) {
        log.info("医网签目前不支持");
    }

}
